package com.kuaidao.sys.service.dictiondry.impl;

import com.github.pagehelper.PageHelper;
import com.kuaidao.common.entity.IdsEntity;
import com.kuaidao.common.mapper.IBaseCRUD;
import com.kuaidao.common.service.AbstractBaseService;
import com.kuaidao.sys.dto.dictionary.DictionaryAddUpdateDTO;
import com.kuaidao.sys.dto.dictionary.DictionaryReqDTO;
import com.kuaidao.sys.dto.dictionary.DictionaryResDTO;
import com.kuaidao.sys.entity.user.Dictionary;
import com.kuaidao.sys.mapper.user.DictionaryEditionMapper;
import com.kuaidao.sys.mapper.user.DictionaryMapper;
import com.kuaidao.sys.service.dictiondry.IDictiondryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
public class DictiondryServiceImpl extends AbstractBaseService<Dictionary, Long> implements IDictiondryService <Dictionary, Long>{


    @Autowired
    private DictionaryMapper dictionaryMapper;


    @Autowired
    private DictionaryEditionMapper dictionaryEditionMapper;

    @Override
    protected IBaseCRUD<Dictionary, Long> getBaseCRUD() {
        return  (IBaseCRUD) dictionaryMapper;
    }

    @Override
    public boolean insertDictiondrt(Dictionary dictionary) {
        return dictionaryMapper.insert(dictionary);
    }

    @Override
    public List<DictionaryResDTO> findDictionaryByPage(DictionaryReqDTO dictionaryReqDTO) {
        PageHelper.startPage(dictionaryReqDTO.getPageNum(),dictionaryReqDTO.getPageSize());

        return dictionaryMapper.findDictiondryByPage(dictionaryReqDTO);
    }

    @Override
    public Dictionary findDictionaryByPrimaryKey(Long primaryKey) {
        return dictionaryMapper.findByPrimaryKey(primaryKey);
    }



    @Override
    @Transactional
    public boolean updateDictionary(Dictionary dictionary) {

        // 1.更新主题  2.在更新词条
        boolean update = dictionaryMapper.updateDictionary(dictionary);

        if (update){
            dictionaryEditionMapper.updateDictionaryEditionByCode(dictionary);
        }

        return true;
    }

    @Override
    @Transactional
    public boolean deleteDictionarys(IdsEntity idsEntity) {
        // 删除主题  删除词条
        boolean b = dictionaryMapper.deleteDictionarys(idsEntity);

        if(b){
            dictionaryEditionMapper.deleteDictionaryEditionById(idsEntity);
        }
        return true;
    }


}
